package com.gray.blog.service.front;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.gray.blog.common.contstant.FrontGameConstant;
import com.gray.blog.common.enums.DelFlagTypeEnum;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.enums.YesOrNoEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.common.util.PagerUtil;
import com.gray.blog.common.util.ResultUtil;
import com.gray.blog.common.util.StringUtil;
import com.gray.blog.dao.front.FrontGameDao;
import com.gray.blog.entity.front.FrontGameEntity;
import com.gray.blog.result.sys.GameDetailResult;
import com.gray.blog.result.sys.GetPageGameResult;
import com.gray.blog.service.BaseService;
import com.gray.blog.vo.sys.AddSysGameVO;
import com.gray.blog.vo.sys.GetPageGameVO;
import com.gray.blog.vo.sys.UpdateGameInfoVO;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created  By zhoux 2019/11/19
 **/
@Service
@Slf4j
public class FrontGameService extends BaseService<FrontGameDao, FrontGameEntity> {

    @Autowired
    private FrontGameDao frontGameDao;

    public ModelResult<GetPageGameResult> getPageGame(GetPageGameVO vo) {
        IPage<FrontGameEntity> pager = super.findItemByPage(vo.getPageNum(), vo.getPageSize(), vo.getKeyWordType(), vo.getKeyWord());
        PagerUtil<GetPageGameResult> pagerUtil = new PagerUtil<>();
        GetPageGameResult result = pagerUtil.setPageResultByPageObject(pager, new GetPageGameResult(), GameDetailResult.class);
        pagerUtil.setPageResultTime(result.getDataList());
        return ModelResult.newSuccess(result);
    }

    public ModelResult addSysGame(AddSysGameVO vo) {
        FrontGameEntity frontGameEntity = BeanUtil.copy(vo, FrontGameEntity.class);
        frontGameEntity.setId(StringUtil.getUUID());
        frontGameEntity.setCreateBy(vo.getUserId());
        frontGameEntity.setUpdateBy(vo.getUserId());
        frontGameEntity.setDelFlag(DelFlagTypeEnum.NOT_DELETE.getType());
        frontGameDao.insert(frontGameEntity);
        return ModelResult.newSuccess();
    }

    public ModelResult deleteSysGame(String id) {
        if (this.deleteById(id)) {
            return new ModelResult<>(ErrorCodeEnum.SUCCESS);
        } else {
            log.warn("FrontGameService.deleteSysGame error id:{}", id);
            return new ModelResult<>(ErrorCodeEnum.SYS_DELETE_GAME_ERROR);
        }
    }
    
    public ModelResult<GameDetailResult> getSysGame(String id) {
        FrontGameEntity frontGameEntity = this.getById(id);
        if (frontGameEntity == null) {
            log.warn("FrontGameService.getSysGame frontGameEntity is null id:{}", id);
            return new ModelResult<>(ErrorCodeEnum.GAME_INFO_NOT_EXIST);
        }
        GameDetailResult gameDetailResult = BeanUtil.copy(frontGameEntity, GameDetailResult.class);
        ResultUtil.buildResultTime(gameDetailResult);
        return ModelResult.newSuccess(gameDetailResult);
    }

    public ModelResult updateGame(UpdateGameInfoVO vo) {
        FrontGameEntity frontGameEntity = new FrontGameEntity();
        frontGameEntity.setId(vo.getId());
        frontGameEntity.setName(vo.getName());
        frontGameEntity.setIntroduction(vo.getIntroduction());
        frontGameEntity.setEvaluation(vo.getEvaluation());
        frontGameEntity.setCover(vo.getCover());
        int updateResult = frontGameDao.update(frontGameEntity);
        if (updateResult != 1) {
            log.error("FrontGameService.updateGame error vo:{}", vo);
            return new ModelResult<>(ErrorCodeEnum.SYSTEM_ERROR);
        }
        return ModelResult.newSuccess();
    }

    public ModelResult<List<GameDetailResult>> getFrontGameInfo() {
        List<GameDetailResult> gameDetailResultList = Lists.newArrayList();
        Map<String, Object> param = Maps.newHashMap();
        param.put("showInFront", YesOrNoEnum.YES.getType());
        List<FrontGameEntity> frontGameEntityList = frontGameDao.getDataLimit(FrontGameConstant.FONT_GAME_LIMIT, param);
        if (CollectionUtils.isEmpty(frontGameEntityList)) {
            return ModelResult.newSuccess(gameDetailResultList);
        }
        gameDetailResultList = BeanUtil.copy(frontGameEntityList, GameDetailResult.class);
        ResultUtil.buildResultTime(gameDetailResultList);
        return ModelResult.newSuccess(gameDetailResultList);
    }

}
